package com.order.controller.admin;

import com.order.config.UpdateRequest;
import com.order.dto.ConsumerDTO;
import com.order.dto.ConsumerPageQueryDTO;
import com.order.entity.User;
import com.order.exception.BaseException;
import com.order.result.PageResult;
import com.order.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import com.order.service.ConsumerService;

import java.util.List;

@RestController
@RequestMapping("/admin/consumer")
@Slf4j
@Api(tags = "客户相关接口")
public class ConsumerController {

    @Autowired
    private ConsumerService consumerService;


    @PostMapping
    @ApiOperation("新增客户")
    public Result save(@RequestBody ConsumerDTO consumerDTO){
        System.out.println("当前线程的id:" + Thread.currentThread().getId());
        consumerService.save(consumerDTO);
        return Result.success();
    }

    @GetMapping("/page")
    @ApiOperation("客户分页查询")
    public Result<PageResult> page(ConsumerPageQueryDTO consumerPageQueryDTO){
        PageResult pageResult = consumerService.pageQuery(consumerPageQueryDTO);
        return Result.success(pageResult);
    }

    @PostMapping("/status/{status}")
    @ApiOperation("启用或禁用客户账号")
    public Result startOrStop(@PathVariable Integer status,@RequestBody UpdateRequest updateRequest){

        Long id = updateRequest.getId();  // 使用包装类型 Long

        String msg = "id不能为空";

        if (id == null) {
            // 处理 id 为 null 的情况
            return Result.error(msg);
        }

        log.info("启用禁用员工账号：{},{}",status,id);

        consumerService.startOrStop(status,id);

        return Result.success();

    }
    @GetMapping("/{id}")
    @ApiOperation("根据客户ID进行查询")
    public Result<User> getById(@PathVariable long id){

        User user = consumerService.getById(id);

        return Result.success(user);
    }

    @PutMapping
    @ApiOperation("编辑客户信息")
    public Result update(@RequestBody ConsumerDTO consumerDTO){
        log.info("编辑员工信息：{}",consumerDTO);

        consumerService.update(consumerDTO);
        return Result.success();
    }

    @DeleteMapping
    @ApiOperation("客户批量删除")
    public Result delete(@RequestParam List<Long> ids){
        log.info("客户批量删除：{}",ids);
        consumerService.deleteUser(ids);

        return Result.success();
    }

}
